<?php
require_once("root.php");

require_once("daoBuilder.php");

function convertToSQL ($schema, $constraints)
{
   $sql = "";

   foreach ($schema as $tableName => $columns)
   {
      $sql .= "drop table if exists `$tableName`;\n";
      $sql .= "create table if not exists `$tableName` (\n";
	$seperator = "";
	foreach ($columns as $column)
	{
		$name = $column["name"];
		$type = $column["type"];
		$length = $column["length"];
		$notNull = $column["not-null"];
		switch ($type) {
			case "string" : $sqlType = "varchar";
				if ($length > 255) {
				   $sqlType = "longtext";
				   $length = -1;
				} break;
			case "int" : $sqlType = "int"; break;
			case "date" : $sqlType = "datetime"; break;
			case "money" : $sqlType = "double"; $length="16,2"; break;
			case "boolean" : $sqlType = "char"; $length="1"; break;
			default : $sqlType = "int"; $length="8"; break;
		}

		$sql .= $seperator . "    " . $name . " " . $sqlType . (($length != -1) ? "(" . $length . ")" : "") . " " . (($notNull == "true") ? "NOT NULL" : "NULL") . " " . ($name == "id" ? "AUTO_INCREMENT" : "");
		$seperator = ",\n";
	}
       // TODO Add Constraints here.
	foreach ($constraints[$tableName] as $constraint)
	{
		if ($constraint["type"] == "pk")
		  $sql .= ",\n    PRIMARY KEY (" . $constraint["column"] . ")";
		if ($constraint["type"] == "fk")
		  $sql .= ",\n    FOREIGN KEY (" . $constraint["foreignTable"] . "_id) REFERENCES `" . $constraint["foreignTable"] . "`(id)";
	}

      $sql .= "\n);\n\n";
   }

   return $sql;

}

function makeCodeLookupInserts ($codeLookupData)
{
	$sql = "";
	foreach ($codeLookupData as $ref => $keyValues)
	{
		foreach($keyValues as $keyValue)
		{
		    $sql .= "insert into code_lookup (name, code_key, value)"
				. " values ('$ref', " . $keyValue['key']
				. ", '" . $keyValue["value"] . "');\n";
		}
	}

	return $sql;
}

function convertToDAO($schema, $dir)
{
	global $ROOT;
	$dir = $dir;
	$includeFiles = array();
   	foreach ($schema as $tableName => $columns)
	{
		$withSpaces = str_replace("_", " ", $tableName);
		$capitalized = ucwords($withSpaces);
		$className = str_replace(" ", "", $capitalized);
		$phpFile = generateDaoAndValueObject($tableName, $className);
		writeToFile($ROOT . $dir . $className . ".php", $phpFile);
		array_push($includeFiles, $className . ".php");
	}
	$daoPhpFile = "<?php\n\n";
	$daoPhpFile .= "  require_once(\"root.php\");\n\n";
	foreach ($includeFiles as $filename)
	{
		$daoPhpFile .= "include_once(\x24ROOT . \"$dir" . "$filename\");\n";
	}
	$daoPhpFile .= "\n?>";

	writeToFile($ROOT .$dir . "dao.php", $daoPhpFile);
}

function writeToFile ($filename, $content)
{

  // Let's make sure the file exists and is writable first.
     if (!$handle = fopen($filename, 'w+')) {
         echo "Cannot open file ($filename)";
         exit;
     }

     if (is_writable($filename))
     {
       // Write $somecontent to our opened file.
       if (fwrite($handle, $content) === FALSE) {
         echo "Cannot write to file ($filename)";
         exit;
       }

       echo "Success, wrote content to file ($filename)\n";
     }

     fclose($handle);

}


?>
